<template>
  <div id="staModuleUpgradeDiv">
    <vxe-toolbar perfect>
      <template #buttons>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="rloading" :disabled="isEmpty(staModule)" @click="findStaModuleUpgradeList"></vxe-button>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新增" :disabled="isEmpty(staModule)" @click="addStaModuleUpgrade"></vxe-button>
        <vxe-button type="text" class="button-blue" icon="vxe-icon-save" content="保存" :disabled="isEmpty(staModule)" @click="saveStaModuleUpgradeList"></vxe-button>
        <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" :disabled="isEmpty(staModule)" @click="deleteStaModuleUpgrade"></vxe-button>
      </template>
    </vxe-toolbar>

    <div class="tableBox">
      <vxe-table ref="staModuleFundesTableRef" class="mytable-scrollbar" border="inner" align="center" height="auto" :row-config="{ isCurrent: true, isHover: true }" :data="staModuleUpgradeList" :loading="rloading" :column-config="{ resizable: true }" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true }" resizable show-overflow keep-source>
        <vxe-column type="checkbox" width="30"></vxe-column>
        <vxe-column field="upgradeCode" title="编号" width="200" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.upgradeCode" placeholder="" type="text"></vxe-input>
          </template>
        </vxe-column>
        <vxe-column field="upgradeDesc" title="功能说明" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.upgradeDesc" placeholder="" type="text"></vxe-input>
          </template>
        </vxe-column>

        <vxe-column field="seq" title="序号" width="150" :edit-render="{}">
          <template #edit="{ row, rowIndex, column }">
            <vxe-input v-model="row.seq" type="number"></vxe-input>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import Breadcrumb from '@/components/Breadcrumb.vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
defineExpose({ initPage });
let staModule = ref(null);

let staModuleUpgradeList: any = reactive([]);
let rloading = ref(false);

function initPage(row: any) {
  staModule.value = row;
  findStaModuleUpgradeList();
}

// 获取左侧仪器表格数据+刷新+右侧表格数据
function findStaModuleUpgradeList() {
  rloading.value = true;
  staModuleUpgradeList.length = 0; //放入数据前清空数据
  if (isEmpty(staModule.value)) {
    rloading.value = false;
    return;
  }
  const url = '/lis/sta/staModuleUpgrade/findStaModuleUpgradeList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: { staModId: staModule.value.id }
    })
    .then((res: any) => {
      staModuleUpgradeList.push(...res.data);
      rloading.value = false;
    })
    .catch((err: any) => {
      rloading.value = false;
      VxeMessage('获取右侧质控项目表格数据异常', VxeNotifyType.error);
    });
}

function addStaModuleUpgrade() {
  if (isEmpty(staModule.value.id)) {
    VxeMessage('请选择仪器', VxeNotifyType.error);
  } else {
    const record: any = {
      id: '',
      staModId: staModule.value.id,
      staModCode: staModule.value.staModCode,
      upgradeCode: '',
      upgradeDesc: '',
      seq: 1
    };
    staModuleUpgradeList.push(record);
  }
}

// 左侧保存
async function saveStaModuleUpgradeList() {
  if (isEmpty(staModule.value.id)) {
    VxeMessage('请选择仪器', VxeNotifyType.error);
  } else {
    let $Xtable = proxy.$refs.staModuleFundesTableRef;
    if ($Xtable) {
      // 表格实例表用该方法拿到有id的为修改
      const updateRecords = await $Xtable.getUpdateRecords().filter((p: any) => {
        return p.id && p.id !== '';
      });
      // 拿到数组中没有id的数据为新增
      const InsertRecords = staModuleUpgradeList.filter((e: any) => {
        return !e.id || e.id === '';
      });

      const sveOrupdateUpgrades = [] as any[];
      updateRecords.forEach((e: any) => {
        const iIUpgrade: any = {
          id: e.id,
          staModId: e.staModId,
          staModCode: e.staModCode,
          upgradeCode: e.upgradeCode,
          upgradeDesc: e.upgradeDesc,
          seq: e.seq,
          orgId: e.orgId
        };

        sveOrupdateUpgrades.push(iIUpgrade);
      });
      InsertRecords.forEach((e: any) => {
        const iIUpgrade: any = {
          id: e.id,
          staModId: e.staModId,
          upgradeCode: e.upgradeCode,
          funCode: e.funCode,
          upgradeDesc: e.upgradeDesc,
          seq: e.seq,
          orgId: e.orgId
        };
        sveOrupdateUpgrades.push(iIUpgrade);
      });

      if (sveOrupdateUpgrades.length > 0) {
        const url = '/lis/sta/staModuleUpgrade/saveOrUpdateStaModuleUpgrade';
        const obj: any = {};
        obj.staModuleUpgradeList = sveOrupdateUpgrades;
        proxy.$http
          .post(url, obj)
          .then((res: any) => {
            VxeMessage('更新成功', VxeNotifyType.success);
            setTimeout(() => {
              findStaModuleUpgradeList();
            }, 500);
          })
          .catch((err: any) => {
            VxeMessage('更新失败', VxeNotifyType.success);
          });
      } else {
        VxeMessage('无添加或修改数据', VxeNotifyType.error);
      }
    } else {
      return;
    }
  }
}
// 左侧删除
function deleteStaModuleUpgrade() {
  const sltUpgrades = (proxy.$refs.staModuleFundesTableRef as any).getCheckboxRecords();
  if (!(sltUpgrades.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        //后台删除数据
        const delStaModuleUpgradeList = sltUpgrades.filter((p: any) => !isEmpty(p.id));
        if (delStaModuleUpgradeList.length > 0) {
          const data = { staModuleUpgradeList: delStaModuleUpgradeList };
          const url = '/lis/sta/staModuleUpgrade/batchDeleteStaModuleUpgradeList';
          proxy.$http
            .post(url, data)
            .then((res: any) => {
              findStaModuleUpgradeList(); // 获取检验仪器表格数据
              VxeMessage(res.status.msg, VxeNotifyType.success);
            })
            .catch((err: any) => {
              VxeMessage(err, VxeNotifyType.error);
            });
        }
        //页面删除未保存的数据
        const removeStaModuleUpgradeList = sltUpgrades.filter((p: any) => isEmpty(p.id));
        if (removeStaModuleUpgradeList.length > 0) {
          // 删除的新增的空白数据 不走接口
          let $Xtable = proxy.$refs.staModuleFundesTableRef;
          if ($Xtable) {
            for (const removeRow of removeStaModuleUpgradeList) {
              let cnt = staModuleUpgradeList.findIndex((item: any) => item == removeRow);
              staModuleUpgradeList.splice(cnt, 1);
            }
          }
        }
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}
</script>

<style lang="scss" scoped>
#staModuleUpgradeDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .tableBox {
    height: calc(100% - 40px);
    background-color: $base-white;
  }
}
</style>
